/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins  
 */
package com.apache.bid.service.cacheplugins;

import java.math.BigDecimal;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.TeaUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.cache.BidDataBaseRunable;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidInfoJingjia;
import com.apache.bid.entity.BidItemPrice;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  荷兰式提交方法
 * @author Hou Dayu 创建时间：2016-3-28  
 * 
 */
public class HeLanSavePluginImpl implements PluginConnector {

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		String objId = String.valueOf(param.getParams("syncObjId"));
		Constant.getInstance().setJingjiaMap(objId, objId);
		final String key = Constant.getInstance().getJingjiaMap(objId);
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		synchronized (key) {//加入同步锁
			String bidTime = DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS();
			String itemId = String.valueOf(param.getParams("itemId"));
			String userEname = String.valueOf(param.getParams("userEname"));
			String myPrice = String.valueOf(param.getParams("myPrice"));
			param.setParams("bidTime", bidTime);
			BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemId);
			BidItemCacheVo itemVo = (BidItemCacheVo) cache.getCacheObjectByKey(cache.item_prefix + itemId);
			String applyKey = "";
			if (Constant.SETYPE_HD.equals(itemVo.getSessionType())) {
				applyKey = BidCacheManager.apply_prefix + userEname + "_" + price.getItemId();
			} else {
				applyKey = BidCacheManager.apply_prefix + userEname + "_" + itemVo.getSessionId();
			}
			BidApply apply = (BidApply) cache.getCacheObjectByKey(applyKey);
			String status = BidCommonUtil.getInstance().getStatus(price, bidTime);
			String myPriceStr = TeaUtil.decryptByTea(myPrice);
			ResultMsg msg = BidCommonUtil.getInstance().getCheckItem(status, myPriceStr, userEname, itemVo, price);
			if (!ToolsUtil.isEmpty(msg)) {
				entity.setResult("false");
				entity.setEntity("F".equals(msg.getFlag()) ? "false" : "true");
				entity.setMessage(msg.getMsg());
				return entity;
			}
			boolean mark = exeBaojia(param, price, itemVo, apply);
			if (mark) {
				entity.setResult("true");
				entity.setEntity("true");
				entity.setMessage("报价成功");
			} else {
				entity.setResult("false");
				entity.setEntity("false");
				entity.setMessage("报价失败");
			}
		}
		return entity;
	}

	private boolean exeBaojia(ParamsVo param, BidItemPrice price, BidItemCacheVo itemVo, BidApply apply)
			throws Exception {
		ResultEntity entity = new ResultEntity();
		String bidTime = String.valueOf(param.getParams("bidTime"));
		//String bidTime = DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS();

		int bidTimes = Integer.parseInt(StrUtil.doNull(price.getBidTimes(), "0"));
		int itemNum = Integer.parseInt(StrUtil.doNull(itemVo.getItemNum(), "1"));
		BigDecimal fall_lowerst_price = new BigDecimal(StrUtil.doNull(itemVo.getFallLowerstPrice(), "0"));
		BigDecimal bidValue = new BigDecimal(StrUtil.doNull(price.getFirstValue(), "0"));
		ParamsVo<BidItemPrice> vo = new ParamsVo<BidItemPrice>();
		if (bidTimes == itemNum || bidValue.compareTo(fall_lowerst_price) < 0) {//判断是否应该结束了
			price.setReductionCloseTime(bidTime);
			price.setCloseTime(bidTime);
			BidCacheFactory.getInstance().getCacheManager("itemPrice")
					.updateCacheObject(BidCacheManager.item_price_prefix + itemVo.getItemId(), price);
			vo.setObj(price);
			BidDataBaseRunable runable2 = new BidDataBaseRunable(vo, "bidItemPriceManager", "update");
			BidCacheFactory.getInstance().exeDatabase(runable2);
			return false;
		}

		String itemId = String.valueOf(param.getParams("itemId"));
		String userEname = String.valueOf(param.getParams("userEname"));
		String myPrice = String.valueOf(param.getParams("myPrice"));
		String bidNum = String.valueOf(param.getParams("bidNum"));
		String userIp = String.valueOf(param.getParams("userIp"));
		boolean mark = false;
		BidInfoJingjia bjVo = new BidInfoJingjia();
		bjVo.setBidValue(myPrice);
		bjVo.setUserIp(userIp);
		BigDecimal myPrices = new BigDecimal(myPrice);
		////////////本次报价涨幅//////////
		bjVo.setIncValue(String.valueOf(myPrices.subtract(new BigDecimal(itemVo.getFirstValuePre()))));
		bjVo.setAmount(String.valueOf(myPrices.multiply(new BigDecimal(itemVo.getItemNum()))));
		bjVo.setBidTime(bidTime);
		bjVo.setRightLevel("0");
		bjVo.setUserName(userEname);
		bjVo.setUserId(apply.getUserId());
		bjVo.setIncValue(String.valueOf(myPrices.subtract(new BigDecimal(price.getFirstValue()))));
		bjVo.setBidId(Validator.generate());
		bjVo.setItemId(itemId);
		bjVo.setBidNum(bidNum);
		mark = BidCacheFactory.getInstance().getCacheManager("jingjia").createCacheObject(bjVo);
		if (mark) {
			ParamsVo<BidInfoJingjia> jvo = new ParamsVo<BidInfoJingjia>();
			jvo.setObj(bjVo);
			BidDataBaseRunable runable = new BidDataBaseRunable(jvo, "bidInfoJingjiaManager", "save");
			BidCacheFactory.getInstance().exeDatabase(runable);
			price.setBidTimes(String.valueOf(bidTimes + 1));
			BidCacheFactory.getInstance().getCacheManager("itemPrice")
					.updateCacheObject(BidCacheManager.item_price_prefix + itemVo.getItemId(), price);
			vo.setObj(price);
			BidDataBaseRunable runable2 = new BidDataBaseRunable(vo, "bidItemPriceManager", "update");
			BidCacheFactory.getInstance().exeDatabase(runable2);
			entity.setResult("true");
			entity.setEntity("true");
			entity.setMessage("报价成功");
		} else {
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage("报价失败");
		}
		return mark;
	}
}
